<script setup>
import { ref, reactive, onMounted } from "vue";
import { getList as getListAPI, getUserFriend } from '../Api/user'
import Navbar from "../components/navbar.vue";
import chatWindow from "../components/chatWindow.vue";
import { useStore } from "../store/useStore";
import { setTimeFormat } from '@/utils/setTimeFormat.js'
import getUser from "./finds/getuser.vue";
const storePro = useStore()
console.log(storePro.userList);
import { useRouter } from 'vue-router';
const router = useRouter()
const store = useStore()
const sum = ref(0)
const selectedImages = ref([])
const show = ref(false);
//多选
const selectClick = (imageSrc) => {
  console.log('点击了');
  const isSelected = selectedImages.value.includes(imageSrc)
  if (isSelected) {
    selectedImages.value = selectedImages.value.filter(item => item !== imageSrc)
    sum.value--
  } else {
    selectedImages.value.push(imageSrc)
    //选中一个sum+1
    sum.value++
  }
}
const btn = () => {
  const data = {
    type: 1,
    data: {
      fromId: storePro.info.id,
      toId: propsItem.id,
      fromAvatar: storePro.info.avatar,
      toAvatar: storePro.friendlist[0].avatar,
      value: selectedImages.value,
      time: new Date().getTime(),
    },
  };
  console.log(data);
  console.log('发送');
  store.ws.send(JSON.stringify(data))
  router.push('/home')
}

const getList = async () => {
  const res = await getListAPI();
  const userFriend = await getUserFriend(res.data.data.id)
  if (userFriend.data.code === 0) {
    storePro.setUserFriendList(userFriend.data.data)
    console.log(storePro.userFriendList, 11111111);
  }
  const forms = res.data.data
  storePro.setInfo(forms)
}

// const list = reactive([
//   {
//     id: 1,
//     img: "https://ts3.cn.mm.bing.net/th?id=OIP-C.y6rdSCGpxbfeb8Rd1CpSuwAAAA&w=250&h=250&c=8&rs=1&qlt=90&o=6&dpr=1.3&pid=3.1&rm=2",
//     title: "实训2309C",
//     describe: "水电费工会经费十分感动v啊啊个粉丝大发而无法的收费方式达尔文认为犬瘟热回复谷歌官方是的发涩",
//     draft: ""
//   },
//   { id: 2, img: "https://ts4.cn.mm.bing.net/th?id=OIP-C.PobTVuzuQy7tAZtvVEc3VQAAAA&w=250&h=250&c=8&rs=1&qlt=90&o=6&dpr=1.3&pid=3.1&rm=2", title: "实训2309C", describe: "水电费工会经费", draft: "" },
//   { id: 3, img: "https://ts4.cn.mm.bing.net/th?id=OIP-C.H9x1BxITI75Tc3IMOKJekwAAAA&w=250&h=249&c=8&rs=1&qlt=90&o=6&dpr=1.3&pid=3.1&rm=2", title: "申高强", describe: "水电费工会经费", draft: "" },
// ]);

const toUser = reactive({})
const showDetails = ref(false);
const openContactDetails = (item) => {
  showDetails.value = true;
  propsItem.value = item; // 传递item数据
  Object.keys(item).forEach(key => {
    toUser[key] = item[key]
  })
  console.log(propsItem, 'propsItem');

};
const propsItem = ref({});
const clearContactDetails = () => {
  showDetails.value = false;
};

const isShow = ref(false)
const closeModal = () => {
  isShow.value = false
}
const isShowTan = () => {
  isShow.value = true
};

//  在发送信息之后，将输入的内容中属于表情的部分替换成emoji图片标签
//  再经过v-html 渲染成真正的图片
const replaceFace = (con) => {
  if (con.includes("/:")) {
    var emojis = storePro.emojis;
    for (var i = 0; i < emojis.length; i++) {
      con = con.replace(
        emojis[i].reg,
        '<img src="static/emoji/' +
        emojis[i].file +
        '"  alt="" style="vertical-align: middle; width: 24px; height: 24px" />'
      );
    }
    return con;
  }
  return con;
}

onMounted(() => {
  getList()
})
</script>

<template>
  <div>
    <Navbar title="微信" showSearch="true" @isShowTan="isShowTan"></Navbar>
    <div class="home">
      <ul class="list">
        <li v-for="item in storePro.friendlist" :key="item.id" @click="openContactDetails(item)">
          <div class="listImg">
            <img :src="item.avatar" alt="" />
          </div>
          <div class="listText">
            <div style="" class="list_top">
              <p class="nickname">{{ item.nickname }}</p>
              <p class="newtime">
                {{
                  setTimeFormat(item.lasttime)
                }}
              </p>
            </div>
            <div style="" class="content">
              <!-- {{
                storePro.list[item.id]?.length > 0
                  ? storePro.list[item.id][storePro.list[item.id].length - 1]
                    .value
                  : ""
              }} -->
              <div v-html="replaceFace(storePro.list[item.id]?.length > 0
                ? storePro.list[item.id][storePro.list[item.id].length - 1]
                  .value
                : '')" class="inputVal"></div>
            </div>
          </div>
        </li>
      </ul>
    </div>
    <div class="ChatWindow">
      <chatWindow :showDetails="showDetails" :toUser="toUser" :propsItem="propsItem"
        @clearContactDetails="clearContactDetails" :show="show"/>
    </div>
  </div>
  <!-- 搜索弹框 -->
  <ModelTan :isShow="isShow" @closeModal="closeModal" />
</template>

<style scoped>
* {
  margin: 0;
  padding: 0;
  list-style: none;
}

.home {
  width: 100%;
  height: calc(100vh - 90px);
  background-color: #e5e5e5;
  /* overflow: auto; */
  overflow: hidden;
}

.list {
  width: 100%;
  height: calc(100% - 50px);
  /* background-color: #000000; */
}

.list li {
  width: 100%;
  height: 60px;
  margin: 10px 0;
  display: flex;
  border-bottom: 1px solid #d6d6d6;
  /* 添加底部边框 */
  padding-bottom: 10px;
}

/* .list li:last-child {
  border: none;
} */

.listImg {
  width: 50px;
  height: 50px;
  margin-left: 10px;
  margin-right: 10px;
  /* background-color: #3fffff; */
}

.listText {
  width: calc(100% - 50px);
  height: 50px;
  /* background-color: #81d48b; */
}

img {
  width: 100%;
  height: 100%;
  border-radius: 7px;
}

.content {
  color: #8a8a8a;
  font-size: 12px;
  width: 280px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  margin-top: 5px;
}

.list_top {
  display: flex;
  justify-content: space-between;
}

.nickname {
  font-size: 16px;
  font-weight: 600;
  color: #000;
}

.newtime {
  margin-right: 10px;
  color: #8a8a8a;
  font-size: 12px;
}
</style>
